#!/bin/bash

meh() {
echo
echo "Failure!"
echo
exit 1
}

RELEASE=$(/usr/bin/lsb_release -rs)

if [ $(/usr/bin/expr "$RELEASE" "<" "11.10") -eq 1 ]
    then
        EXTENSION_PATH="$HOME/.nautilus/python-extensions"
    else
        EXTENSION_PATH="$HOME/.local/share/nautilus-python/extensions"
fi

TEMPLATES_PATH=$(/usr/bin/xdg-user-dir TEMPLATES)

if [ ! -d "$EXTENSION_PATH" ]
    then
        echo
        echo -e "Creating extension directory:\n\t$EXTENSION_PATH"
        mkdir -p "$EXTENSION_PATH" || meh
fi
echo
echo "Copying extension ..."; cp -iv FolderTemplate.py $EXTENSION_PATH || meh
echo
echo "Copying language file ..."; cp -iv FolderTemplate.lang $EXTENSION_PATH || meh
echo
echo "Create example template?"
select yn in "Yes" "No"; do
    case $yn in
        Yes )
            if [ ! -d "$TEMPLATES_PATH" ]
                then
                    echo "Creating template directory: $TEMPLATES_PATH"
                    mkdir -pv "$TEMPLATES_PATH" || meh
            fi
            echo -e "Creating example template directory:\n\t$TEMPLATES_PATH/Example template"
            mkdir -p "$TEMPLATES_PATH/Example template" || meh
            echo -e "Creating '.is_template' file:\n\t$TEMPLATES_PATH/Example template/.is_template"
            touch "$TEMPLATES_PATH/Example template/.is_template" || meh
            echo -e "Creating example content:\n\t$TEMPLATES_PATH/Example template/Example file"
            touch "$TEMPLATES_PATH/Example template/Example file" || meh
            break;;
        No )
            break;;
    esac
done
echo
echo "Restarting nautilus ..."
killall -v nautilus
echo
echo Success!
echo
exit 0

